//
//  ViewController.m
//  JSONModel
//
//  Created by pengpeng on 2024/1/25.
//

#import "ViewController.h"
#import "AppManager.h"

@interface ViewController ()

/// 输入的json
@property (nonatomic, strong) UITextView *jsonTextView;
/// 输出的模型
@property (nonatomic, strong) UITextView *modelTextView;

/// 操作区域
@property (nonatomic, strong) UIStackView *operationView;
@property (nonatomic, strong) UIButton *pasteBtn;
@property (nonatomic, strong) UIButton *generBtn;
@property (nonatomic, strong) UIButton *copyBtn;

@property (nonatomic, strong) UILabel *tipsLabel;

@end

@implementation ViewController

- (UITextView *)jsonTextView {
    if (!_jsonTextView) {
        _jsonTextView = [[UITextView alloc] init];
        _jsonTextView.autocorrectionType = UITextAutocorrectionTypeNo;
        _jsonTextView.spellCheckingType = UITextSpellCheckingTypeNo;
        _jsonTextView.font = [UIFont systemFontOfSize:18];
        _jsonTextView.smartQuotesType = UITextSmartQuotesTypeNo;
//        _jsonTextView.palceholdertextView.font = [UIFont systemFontOfSize:18];
//        _jsonTextView.placeholderGlobal = @"请输入json";
        _jsonTextView.layer.cornerRadius = 4;
        _jsonTextView.layer.borderWidth = 1;
        _jsonTextView.layer.borderColor = UIColor.lightGrayColor.CGColor;
    }
    return _jsonTextView;
}

- (UITextView *)modelTextView {
    if (!_modelTextView) {
        _modelTextView = [[UITextView alloc] init];
        _modelTextView.autocorrectionType = UITextAutocorrectionTypeNo;
        _modelTextView.spellCheckingType = UITextSpellCheckingTypeNo;
        _modelTextView.font = [UIFont systemFontOfSize:18];
        _modelTextView.smartQuotesType = UITextSmartQuotesTypeNo;
//        _modelTextView.palceholdertextView.font = [UIFont systemFontOfSize:18];
//        _modelTextView.placeholderGlobal = @"输出模型";
        _modelTextView.layer.cornerRadius = 4;
        _modelTextView.layer.borderWidth = 1;
        _modelTextView.layer.borderColor = UIColor.lightGrayColor.CGColor;
    }
    return _modelTextView;
}

- (UIButton *)pasteBtn {
    if (nil == _pasteBtn) {
        _pasteBtn = [[UIButton alloc] init];
        [_pasteBtn setTitle:@"粘贴" forState:UIControlStateNormal];
        [_pasteBtn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
        [_pasteBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_pasteBtn addTarget:self action:@selector(doPasteBtnClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pasteBtn;
}

- (UIButton *)generBtn {
    if (nil == _generBtn) {
        _generBtn = [[UIButton alloc] init];
        [_generBtn setTitle:@"生成" forState:UIControlStateNormal];
        [_generBtn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
        [_generBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_generBtn addTarget:self action:@selector(doGenerateBtnClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _generBtn;
}

- (UIButton *)copyBtn {
    if (nil == _copyBtn) {
        _copyBtn = [[UIButton alloc] init];
        [_copyBtn setTitle:@"复制" forState:UIControlStateNormal];
        [_copyBtn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
        [_copyBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_copyBtn addTarget:self action:@selector(doCopyBtnClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _copyBtn;
}

- (UIStackView *)operationView {
    if (nil == _operationView) {
        _operationView = [[UIStackView alloc] init];
        _operationView.axis = UILayoutConstraintAxisHorizontal;
        _operationView.spacing = 10;
        _operationView.distribution = UIStackViewDistributionFill;
    }
    return _operationView;
}

- (UILabel *)tipsLabel {
    if (nil == _tipsLabel) {
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.numberOfLines = 0;
        _tipsLabel.text = @"在上方输入正确格式的json, 点击生成按钮, 最后可将模型复制到粘贴板";
        _tipsLabel.textColor = UIColor.redColor;
        _tipsLabel.font = [UIFont systemFontOfSize:16];
    }
    return _tipsLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self setupSubviews];
    [self setupOperationView];
}

- (void)setupSubviews {
    [self.view addSubview:self.jsonTextView];
    [self.view addSubview:self.modelTextView];

    UIView *operaBGView = [UIView new];
    [self.view addSubview:operaBGView];

    [self.jsonTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.top.equalTo(self.view.mas_topMargin).offset(8);
        make.right.mas_equalTo(-8);
    }];
    [self.modelTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.bottom.equalTo(self.view.mas_bottomMargin).offset(-8);
        make.right.mas_equalTo(-8);
        make.height.equalTo(self.jsonTextView);
    }];

    [operaBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-8);
        make.top.equalTo(self.jsonTextView.mas_bottom).offset(8);
        make.bottom.equalTo(self.modelTextView.mas_top).offset(-8);
        make.height.mas_greaterThanOrEqualTo(44);
        make.height.mas_lessThanOrEqualTo(88);
    }];

    [operaBGView addSubview:self.operationView];
    [self.operationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-8);
        make.centerY.height.equalTo(operaBGView);
    }];

    [operaBGView addSubview:self.tipsLabel];
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.centerY.height.equalTo(operaBGView);
        make.right.equalTo(self.operationView.mas_left).offset(-8);
    }];
}

- (void)setupOperationView {
    for (UIView *view in self.operationView.subviews) {
        [view removeFromSuperview];
    }

    [self.operationView addArrangedSubview:self.pasteBtn];
    [self.operationView addArrangedSubview:self.generBtn];
    [self.operationView addArrangedSubview:self.copyBtn];
    [self.pasteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
    }];
    [self.generBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
    }];
    [self.copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
    }];
}

#pragma mark - action

- (void)doGenerateModelWithJsonString:(NSString *)jsonString {
    self.modelTextView.text = @"";

    [self doShowMessage:@""];
    if (nil == jsonString || jsonString.length == 0) {
        return;
    }
    // 判断json是否格式正确
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    if (nil == jsonData || jsonData.length == 0) {
        return;
    }
    NSError *jsonError = nil;
    NSObject *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
    if (jsonError) {
        NSLog(@"json格式错误");
        [self doShowMessage:[NSString stringWithFormat:@"json格式错误: %@", jsonError.description]];
        return;
    }

    NSError *prettyError = nil;
    NSData *prettyData = [NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:&prettyError];
    if (jsonError) {
        NSLog(@"json格式化错误");
        [self doShowMessage:[NSString stringWithFormat:@"json格式化错误: %@", jsonError.description]];
        return;
    }
    if (prettyData.length > 0) {
        NSString *prettyString = [[NSString alloc] initWithData:prettyData encoding:NSUTF8StringEncoding];
        if (prettyString.length > 0) {
            self.jsonTextView.text = prettyString;
        }
    }

    NSMutableString *modelString = [NSMutableString string];
    NSString *string = [AppManager.sharedAppManager doGenerateModelWithObject:jsonObject];
    if (string.length > 0) {
        [modelString appendFormat:@"%@\n", string];
    }

    if (modelString.length > 0) {
        [self doShowMessage:@"生成模型成功, 使用前请确认信息是否正确"];
        self.modelTextView.text = modelString;
    }
}

- (void)doPasteBtnClickedAction:(UIButton *)sender {
    NSString *preJsonString = UIPasteboard.generalPasteboard.string;
    self.jsonTextView.text = preJsonString;
    [self doGenerateModelWithJsonString:preJsonString];
}

- (void)doGenerateBtnClickedAction:(UIButton *)sender {
    [self doGenerateModelWithJsonString:self.jsonTextView.text];
}

- (void)doCopyBtnClickedAction:(UIButton *)sender {
    UIPasteboard.generalPasteboard.string = self.modelTextView.text;
    [self doShowMessage:@"已复制到剪切板"];
}

- (void)doShowMessage:(NSString *)string {
    self.tipsLabel.text = string;
}

@end
